Skip to content

Conversation

@erickreutz
Copy link

@erickreutz erickreutz commented Oct 26, 2025

Summary

This PR adds native support for the MCP protocol's _meta parameter, eliminating the need for manual extraction in controllers.

Background

The MCP specification defines a _meta field that allows clients to pass request-specific metadata. Previously, Ruby SDK users had to manually extract this field from request parameters.

Changes

  • Automatic extraction: The server now automatically extracts _meta from request parameters in call_tool and get_prompt methods
  • Nested structure: _meta is passed as a nested field within server_context (accessible via server_context[:_meta])
  • Compatibility: This implementation matches the TypeScript and Python SDKs, which also nest _meta within the context
  • Efficient context creation: Context is only created when there's either server_context or _meta present

Testing

  • Added comprehensive tests for _meta extraction and nesting behavior
  • All tests pass with the new implementation
  • Provider-agnostic test examples (no vendor-specific references)

Documentation

  • Updated README with _meta usage examples
  • Added link to official MCP specification
  • Included both access patterns and client request examples

Usage Example

class MyTool < MCP::Tool
  def self.call(message:, server_context: nil)
    # Access request-specific metadata
    session_id = server_context&.dig(:_meta, :session_id)
    
    # Access server's original context
    user_id = server_context&.dig(:user_id)
    
    MCP::Tool::Response.new([{
      type: "text",
      text: "Processing for user #{user_id} in session #{session_id}"
    }])
  end
end

Breaking Changes

None - this is backwards compatible. Tools that don't use server_context or don't access _meta will continue to work unchanged.

Notes

While implementing this feature, we discovered that the README incorrectly states that server_context is passed to exception and instrumentation callbacks, though the actual implementation only passes contextual error information to these callbacks.

The MCP protocol specification includes a _meta parameter that allows
clients to pass request-specific metadata. This commit adds automatic
extraction of this parameter and makes it available to tools and prompts
as a nested field within server_context.

Key changes:
- Extract _meta from request params in call_tool and get_prompt methods
- Pass _meta as a nested field in server_context (server_context[:_meta])
- Only create context when there's either server_context or _meta present
- Add comprehensive tests for _meta extraction and nesting
- Update documentation with _meta usage examples and link to spec

This maintains compatibility with TypeScript and Python SDKs which also
nest _meta within the context rather than merging it at the top level.
@erickreutz erickreutz force-pushed the add-meta-extraction-support branch from fdb400a to ad9677a Compare October 26, 2025 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant